package org.wwy.demo.excel.test;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import com.alibaba.fastjson.JSON;
import nl.fountain.xelem.XSerializer;
import nl.fountain.xelem.XelemException;
import nl.fountain.xelem.excel.Cell;
import nl.fountain.xelem.excel.Row;
import nl.fountain.xelem.excel.Workbook;
import nl.fountain.xelem.excel.Worksheet;
import nl.fountain.xelem.excel.ss.XLWorkbook;
import nl.fountain.xelem.lex.ExcelReader;
import org.junit.Test;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;

public class HelloExcel {

    public static void main(String[] args) throws XelemException, ParserConfigurationException, SAXException, IOException {
        ExcelReader excelReader = new ExcelReader();
        Workbook workbook = excelReader.getWorkbook("d:/1.xml");
//        List<String> sheetNameList = workbook.getSheetNames();

        /*Worksheet worksheet = workbook.getWorksheet(sheetNameList.get(0));
        List<Map<String, Object>> list = new ArrayList<>();
        Collection<Row> rows = worksheet.getRows();
        for (int i = 1; i <= rows.size(); i++) {
            Row row = worksheet.getRowAt(i);
            Collection<Cell> cells = row.getCells();
            int cellSize = 10;
            Map<String, Object> map = new HashMap<>();
            for (int j = 1; j <= cellSize; j++) {
                Cell cell = row.getCellAt(j);
                Object data = cell.getData();
                System.out.println(data);
            }
            System.out.println("---------------------------------");
        }*/
        new XSerializer().serialize(workbook,new File("d:/111.xlsx"));
        Collection<String> warnings = workbook.getWarnings();
        System.out.println("Created '" + workbook.getFileName()
                + "' with " + warnings.size()
                + (warnings.size() == 1 ? " warning." : " warnings."));
        for (String s : warnings) {
            System.out.println(s);
        }

    }

    @Test
    public void test2(){
        String a = "{'abc':'sdfsdfsd\\dfdf'}";
        Map map = JSON.parseObject(a, Map.class);
//        Map map = new TreeMap();
//        map.put("abc","sdfsdfsd\ndfdf");
        System.out.println(map);
        System.out.println(JSON.toJSONString(map));
        map = JSON.parseObject(JSON.toJSONString(map), Map.class);
        System.out.println(map);
    }

    @Test
    public void test3(){
        Pattern pattern = Pattern.compile(".*\n电风扇", Pattern.DOTALL);
        Matcher matcher = pattern.matcher("方式\n电风扇");
        if (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}
